<template>
    <div class="sku">
        <el-card>
            <el-table border :data="skuInfoList">
                <el-table-column label="序号" align="center" type="index" width="80">
                </el-table-column>
                <el-table-column label="名称" prop="skuName" width="150">

                </el-table-column>
                <el-table-column label="描述" prop="skuDesc" width="150">

                </el-table-column>
                <el-table-column label="默认图片" width="150">
                    <template #default="scope">
                        <img :src="scope.row.skuDefaultImg" style="width:100px;height: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="重量(g)" prop="weight" width="150">

                </el-table-column>
                <el-table-column label="价格(元)" prop="price" width="150">

                </el-table-column>
                <el-table-column label="操作" width="220" fixed="right">
                    <template #default="scope">
                        <el-button :type="scope.row.launch ? 'success' : 'info'"
                            :icon="scope.row.launch ? 'Top' : 'Bottom'" size="small"
                            @click="isLaunch(scope.row)"></el-button>
                        <el-button type="primary" icon="Edit" size="small"></el-button>
                        <el-button type="info" icon="Warning" size="small"
                            @click="showSkuInfo(scope.row.id)"></el-button>
                        <el-button type="danger" icon="Delete" size="small"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
                layout="prev, pager, next, jumper,->,sizes,total" :total="total" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" />
            <el-drawer v-model="drawer" direction="rtl">
                <template #header>
                    <h3>查看商品详情</h3>
                </template>
                <template #default>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">名称</el-col>
                        <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">描述</el-col>
                        <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">价格</el-col>
                        <el-col :span="18">{{ skuInfo.price }}</el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">平台属性</el-col>
                        <el-col :span="18">
                            <el-tag style="margin: 5px 5px;" v-for="item in skuInfo.skuAttrValueList" :key="item.id">{{
                                item.attrName }}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">销售属性</el-col>
                        <el-col :span="18">
                            <el-tag style="margin: 5px 5px;" v-for="item in skuInfo.skuSaleAttrValueList
                            " :key="item.id">
                                {{ item.saleAttrName }}
                            </el-tag>
                        </el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">商品图片</el-col>
                        <el-col :span="18">
                            <el-carousel :interval="4000" type="card" height="100px">
                                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                    <img :src="item.imgUrl" style="background-size: cover">
                                </el-carousel-item>
                            </el-carousel>
                        </el-col>
                    </el-row>
                </template>
            </el-drawer>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { reqSkuList, reqOnSale, reqCancelSale, reqSkuInfo } from '@/api/product/sku/index'
import { ElMessage } from 'element-plus';
const currentPage = ref(1)
const pageSize = ref(5)
const total = ref(0)
const skuInfoList = ref([])
const drawer = ref(false)
const skuInfo = ref<any>({})
onMounted(() => {
    getSkuList()
})
const getSkuList = async () => {
    const res = await reqSkuList(currentPage.value, pageSize.value)
    if (res.code == 200) {
        skuInfoList.value = res.data.records
        total.value = res.data.total
    }
}
const handleSizeChange = () => {
    getSkuList()

}
const handleCurrentChange = () => {
    getSkuList()

}
const isLaunch = async (row: any) => {
    row.launch = !row.launch
    if (row.launch) {
        const res = await reqOnSale(row.id)
        if (res.code == 200) {
            ElMessage({
                type: "success",
                message: '上架成功'
            })
        }
    } else {
        const res = await reqCancelSale(row.id)
        if (res.code == 200) {
            ElMessage({
                type: "success",
                message: '下架成功'
            })
        }
    }
}
const showSkuInfo = async (id: string) => {
    drawer.value = true
    const res = await reqSkuInfo(id)
    console.log(res);
    if (res.code == 200) {
        skuInfo.value = res.data
    }
}
</script>

<style lang="scss" scoped>
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
</style>